/*------------------------------------------------------------------------------
 * Fortress DS
 * 
 * Copyright (c) 2008 David Panseri
 * distributed under the terms of the GNU General Public License
 -------------------------------------------------------------------------------
 *
 * TextBackground.cpp
 *
 -----------------------------------------------------------------------------*/

#include <nds.h>
#include <stdio.h>
#include <TextBackground.h>
#include <string.h>

TextBackground::TextBackground()
{
    height = 0;
    width = 0;
    pTiles = NULL;
    tileSize = 0;
    pPalette = NULL;
    palSize = 0;
    pMap = NULL;
    mapSize = 0;
}

TextBackground::TextBackground(u16 height, u16 width, u8* tiles, u16 tileSize, u8* palette, u16 palSize, u16* map, u16 mapSize)
{
    this->height = height;
    this->width = width;
    pTiles = (u8*)tiles;
    this->tileSize = tileSize;
    pPalette = (u8*)palette;
    this->palSize = palSize;
    pMap = (u16*)map;
    this->mapSize = mapSize;
}

//---------- Get Methods ----------
u16 TextBackground::getHeight(void)
{
    return(height);
}

u16 TextBackground::getWidth(void)
{
    return(width);
}

u8* TextBackground::getTiles(void)
{
    return((u8*)pTiles);
}

u16 TextBackground::getTileSize(void)
{
    return(tileSize);
}

u8* TextBackground::getPalette(void)
{
    return((u8*)pPalette);
}

u16 TextBackground::getPalSize(void)
{
    return(palSize);
}

u16* TextBackground::getMap(void)
{
    return((u16*)pMap);
}

u16 TextBackground::getMapSize(void)
{
    return(mapSize);
}

//---------- Set Methods ----------

void TextBackground::setHeight(u16 height)
{
    this->height = height;
}

void TextBackground::setWidth(u16 width)
{
    this->width = width;
}

void TextBackground::setTiles(u8* tiles, u16 size)
{
    pTiles = tiles;
    tileSize = size;
}

void TextBackground::setPalette(u8* palette, u16 size)
{
    pPalette = palette;
    palSize = size;
}

void TextBackground::setMap(u16* map, u16 size)
{
    pMap = map;
    mapSize = size;
}
    

//---------- Copy Methods ----------


/*******************************************************************************
 * Function: copyTiles(void *pTileRam)
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Description: 
 * Copy the tile data from this background to VRAM.
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Parameters:
 * void *pTileRam - Pointer to VRAM where tile data will be stored
 ******************************************************************************/
void TextBackground::copyTiles(void *pTileRam)
{
    memcpy(pTileRam, pTiles, tileSize);
}

/*******************************************************************************
 * Function: copyPalette(void *pPalRam)
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Description: 
 * Copy the palette data from this background to VRAM.
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Parameters:
 * void *pPalRam - Pointer to VRAM where palette data will be stored
 ******************************************************************************/
void TextBackground::copyPalette(void *pPalRam)
{
    memcpy(pPalRam, pPalette, palSize);
}

/*******************************************************************************
 * Function: copyMap(void *pMapRam, u16 x, u16 y)
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Description: 
 * Copy the map data, starting at coordinates (x,y),from this background to the
 * map RAM.
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Parameters:
 * void *pMapRam - Pointer to map ram where the map data will be stored
 * u16 x - Starting x coordinate. Indicates where in the map we need to start 
 * copying from.
 * u16 y - Starting y coordinate. Indicates where in the map we need to start 
 * copying from.
 ******************************************************************************/
void TextBackground::copyMap(void *pMapRam, u16 x, u16 y)
{
    u16* pMapBaseRam = (u16*)pMapRam;
    int i, ix, iy;
    
    for ( i = 0; i < SCR_WIDTH * SCR_HEIGHT; i++)
    {
        ix = i % SCR_WIDTH;
        iy = i / SCR_WIDTH;
        pMapBaseRam[((iy)*DS_32x32_WIDTH) + (ix)] = 
           pMap[(y+iy)*width + (x+ix)%width]; 
    }
}


/*******************************************************************************
 * Function: copyMap(void *pMapRam, u16 x, u16 y, u16 xOffset, u16 yOffset)
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Description: 
 * Copy the map data, starting at coordinates (x,y),from this background to the
 * map RAM.
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Parameters:
 * void *pMapRam - Pointer to map ram where the map data will be stored
 * u16 x - Starting x coordinate. Indicates where in the map we need to start 
 * copying from.
 * u16 y - Starting y coordinate. Indicates where in the map we need to start 
 * copying from.
 * u16 xOffset - offset to add to the x coordinate. Usefull for syncing top and 
 * bottom screens
 * u16 yOffset - offset to add to the y coordinate. Usefull for syncing top and 
 * bottom screens
 ******************************************************************************/
void TextBackground::copyMap(void *pMapRam, u16 x, u16 y, u16 xOffset, u16 yOffset)
{
    u16* pMapBaseRam = (u16*)pMapRam;
    int i, ix, iy;
    
    for ( i = 0; i < SCR_WIDTH * SCR_HEIGHT; i++)
    {
        ix = i % SCR_WIDTH;
        iy = i / SCR_WIDTH;
        pMapBaseRam[((iy)*DS_32x32_WIDTH) + (ix)] = 
           pMap[(y+iy+yOffset)*width + (x+ix+xOffset)%width]; 
    }
}